package cn.edu.hbwe.book.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import cn.edu.hbwe.book.entity.Orders;
import cn.edu.hbwe.book.entity.User;
import cn.edu.hbwe.book.utils.C3P0Util;

public class OrderDao {

	
	private static List<Orders> olist = new ArrayList<Orders>();
	private static QueryRunner run = new QueryRunner(C3P0Util.getDataSource());
	
	// 生成订单
	public void addOrder(Orders order) throws SQLException {
		String sql = "insert into orders(id,money,receieverName,receieverAddress,receieverPhone,paystate,user_id)"
				+ "values(?,?,?,?,?,?,?)";
		Object[] params = { order.getId(), order.getMoney(), order.getReceieverName(), order.getReceieverAddress(),
				order.getReceieverPhone(), order.getPaystate(), order.getUser() };
		int row = run.update(sql, params);
		if (row == 0) {
			throw new RuntimeException();
		}
	}


	//查找用户所属订单
	public List<Orders> findOrderByUser(final User user) throws SQLException {
		String sql = "select * from orders where user_id=?";
		return run.query(sql, new ResultSetHandler<List<Orders>>() {
			public List<Orders> handle(ResultSet rs) throws SQLException {
				List<Orders> orders = new ArrayList<Orders>();
				while (rs.next()) {
					Orders order = new Orders();
					order.setId(rs.getString("id"));
					order.setMoney(rs.getDouble("money"));
					order.setReceieverName(rs.getString("receieverName"));
					order.setReceieverAddress(rs.getString("receieverAddress"));
					order.setReceieverPhone(rs.getString("receieverPhone"));
					order.setPaystate(rs.getInt("paystate"));
					order.setOrderTime(rs.getDate("orderTime"));
					order.setUser(user);
					orders.add(order);
				}
				return orders;
			}
		}, user.getId());
	}

	// 根据id查找订单信息
	public Orders findOrderById(String id) throws SQLException {
		String sql = "select * from orders,user where orders.user_id=user.id and orders.id=?";
		return run.query(sql, new ResultSetHandler<Orders>() {
			public Orders handle(ResultSet rs) throws SQLException {
				Orders order = new Orders();
				while (rs.next()) {
					order.setId(rs.getString("orders.id"));
					order.setMoney(rs.getDouble("orders.money"));
					order.setOrderTime(rs.getDate("orders.orderTime"));
					order.setPaystate(rs.getInt("orders.paystate"));
					order.setReceieverAddress(rs.getString("orders.receieverAddress"));
					order.setReceieverName(rs.getString("orders.receieverName"));
					order.setReceieverPhone(rs.getString("orders.receieverPhone"));

					User user = new User();
					user.setId(rs.getInt("user.id"));
					user.setEmail(rs.getString("user.email"));
					user.setGender(rs.getString("user.gender"));
					user.setActiveCode(rs.getString("user.activecode"));
					user.setDescription(rs.getString("user.description"));
					user.setPassword(rs.getString("user.password"));
					user.setRegisTime(rs.getDate("user.regisTime"));
					user.setRole(rs.getString("user.role"));
					user.setState(rs.getInt("user.state"));
					user.setTelephone(rs.getString("user.telephone"));
					user.setUsername(rs.getString("user.username"));
					order.setUser(user);
				}
				return order;
			}
		}, id);
	}

	// 生成订单
	public void addBook(Orders order) throws SQLException {
		// 生成Sql语句
		String sql = "insert into orders values(?,?,?,?,?,0,null,?)";
		// 执行update()方法插入数据
		run.update(sql, order.getId(), order.getMoney(), order.getReceieverAddress(), order.getReceieverName(),
				order.getReceieverPhone(), order.getUser().getId());
	}

	// 根据id删除订单
	public void delOrderById(String id) throws SQLException {
		String sql = "delete from orders where id=?";
		run.update(sql, id);
	}

	
	//根据订单号修改订单状态
	public void updateOrderState(String id) throws SQLException {
		String sql = "update orders set paystate=1 where id=?";
		run.update(sql, id);
	}

}
